#!/usr/bin/env python  
#coding=utf-8  


# 第一行图片信息数据格式如下:
# ['const', 'unsigned', 'char', 'gImage_SingalmgNullRed[3082]', '=', '{', '0X00,0X10,0X35,0X00,0X1D,0X00,0X01,0X1B,\n']
class ImgDataProcess:
    def _init_(self):
        pass
        # 比较的源文件信息
        # self.SouceDataPath = "SingalImgNullRed.h"

        # 初始化源图片信息
        self.SouceImgName = 0
        self.SouceImgInfo_Scan = 0
        self.SouceImgInfo_Gray = 0
        self.SouceImgInfo_Width = 0
        self.SouceImgInfo_Height = 0
        self.SouceImgInfo_RGBMode = 0
        self.SouceImgInfo_RGB = 0

        # 初始化差异图片信息
        self.DiffImgName = 0
        self.DiffImgInfo_Scan = 0
        self.DiffImgInfo_Gray = 0
        self.DiffImgInfo_Width = 0
        self.DiffImgInfo_Height = 0
        self.DiffImgInfo_RGBMode = 0
        self.DiffImgInfo_RGB = 0


    # 读取图像信息解析
    # 输入：
    #       firstLinestr：取模后图想的首行数据

    def ReadImgInfo(self,firstLinestr):
        try:
            # 字符串按照空格分开
            firstLinestrsplit = firstLinestr.split(" ")
            # 图像数组名
            ImgName = firstLinestrsplit[3].split("[")[0]

            # 图像各部分信息
            # 逗号分割解析 ['0X00', '0X10', '0X35', '0X00', '0X1D', '0X00', '0X01', '0X1B', '\n']
            ImgInfo = firstLinestrsplit[-1].split(",")
            # 扫描模式
            ImgInfo_Scan =  int(ImgInfo[0],16)
            # 灰度值gray
            ImgInfo_Gray = int(ImgInfo[1],16)
            # 图像宽度，单位（px）
            ImgInfo_Width = int(ImgInfo[3],16)*256+int(ImgInfo[2],16)
            # 图像高度，单位（px）
            ImgInfo_Height = int(ImgInfo[5],16)*256+int(ImgInfo[4],16)
            # 图像颜色模式
            ImgInfo_RGBMode = int(ImgInfo[6],16)
            # 图像RGB颜色分量排列顺序
            ImgInfo_RGB = int(ImgInfo[7],16)

            print("""
            ImgInfo:{}
            ImgInfo_Scan:{}
            ImgInfo_Gray:{}
            ImgInfo_Width:{}
            ImgInfo_Height:{}
            ImgInfo_RGBMode:{}
            ImgInfo_RGB:{}
            """.format(ImgName,ImgInfo_Scan,ImgInfo_Gray,ImgInfo_Width,ImgInfo_Height,ImgInfo_RGBMode,ImgInfo_RGB))

            return ImgName,ImgInfo_Scan,ImgInfo_Gray,ImgInfo_Width,ImgInfo_Height,ImgInfo_RGBMode,ImgInfo_RGB
        except:
            print("图像信息解析失败")
    
    # 比较两个图像数据。找到图像颜色存在差异的像素点位置
    # 输入：
    #       SouceImg：原始图像，即被比较的图像数据
    #       DiffImg： 差异图像，即要比较的图像数据，找出差异的像素点
    def CompareImg(self,SouceImg,DiffImg,ImgWidth,ImgHeigth):
        try:
            # 计算两个图像长度是否一样，不一样则停止比较,从第二行数据开始
            if len(SouceImg) != len(DiffImg):
                print("两张图像长度不一致")
                return
            # 参数初始化
            PixelCount = 0           #记录轮询的像素个数
            DiffPixelList = []     #记录记录颜色差异像素点的位置
            # 逐行数据比较两张图像，找出存在颜色差异的像素点位置
            for line in range(1,len(SouceImg)):
                # 查找一行中的差异数据
                for datacount in range(0,len(SouceImg[line]),2):
                    if datacount+1 < len(SouceImg[line]):
                        # 原图像的RGB565低字节和高字节
                        SouceImgrgbLStr =  SouceImg[line][datacount]
                        SouceImgrgbHStr =  SouceImg[line][datacount+1]
                        # print(datacount,SouceImgrgbLStr,SouceImgrgbHStr)

                        # 差异图像的RGB565低字节和高字节
                        DiffImgrgbLStr =  DiffImg[line][datacount]
                        DiffImgrgbHStr =  DiffImg[line][datacount+1]
                        # print(datacount,DiffImgrgbLStr,DiffImgrgbHStr)

                        # 判断两个字节的颜色数据，只要有一个字节不一致则记录该像素点的坐标值
                        if SouceImgrgbLStr!=DiffImgrgbLStr or SouceImgrgbHStr!=DiffImgrgbHStr:
                            print("x:{},y:{},Souce:{} {},diff:{} {}".format(int(PixelCount%ImgWidth),int(PixelCount/ImgWidth),
                            SouceImgrgbLStr,SouceImgrgbHStr,DiffImgrgbLStr,DiffImgrgbHStr))
                            DiffPixelList.append("{%d,%d}"%(int(PixelCount%ImgWidth),int(PixelCount/ImgWidth)))
                        PixelCount += 1
                        
                    # else:
                    #     print(datacount,SouceImg[line][datacount])
            print(PixelCount,53*29)
            print(DiffPixelList,len(DiffPixelList))

            # 返回差异像素点的位置列表。
            return DiffPixelList
        except:
            print("图像比较失败")

    # 打开文件并读取文件内容
    def ReadImgFile(self,filepath):
        # 打开文件，只读模式
        mSouceDataFile = open(filepath,"r",encoding="utf-8")
        # 逐行读取数据
        mSouceData = mSouceDataFile.readlines()
        # 关闭文件，避免占用
        mSouceDataFile.close()

        # 将分割的数据返回
        return mSouceData





# if __name__ == "__main__":
#     Souce = ImgDataProcess()

#     SouceDataPath = "SingalImgNullRed.h"
#     DiffDataPath = "SingalImgNullRedDiff.h"

#     SouceData = Souce.ReadImgFile(SouceDataPath)
#     DiffData = Souce.ReadImgFile(DiffDataPath)

#     # 输入第一行数据，分析图像信息
#     Souce.SouceImgName,Souce.SouceImgInfo_Scan,Souce.SouceImgInfo_Gray,Souce.SouceImgInfo_Width,Souce.SouceImgInfo_Height,Souce.SouceImgInfo_RGBMode,Souce.SouceImgInfo_RGB = Souce.ReadImgInfo(SouceData[0])
#     Souce.DiffImgName,Souce.DiffImgInfo_Scan,Souce.DiffImgInfo_Gray,Souce.DiffImgInfo_Width,Souce.DiffImgInfo_Height,Souce.DiffImgInfo_RGBMode,Souce.DiffImgInfo_RGB = Souce.ReadImgInfo(DiffData[0])

#     # 将各行数据按照","分割
#     SouceDataSplit=[i.split(",") for i in SouceData]
#     DiffDataSplit=[i.split(",") for i in DiffData]

#     Souce.CompareImg(SouceDataSplit,DiffDataSplit,Souce.SouceImgInfo_Width,Souce.SouceImgInfo_Height)
